package cn.iocoder.yudao.module.polling.service.pollingcategories;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.polling.controller.admin.pollingcategories.vo.*;
import cn.iocoder.yudao.module.polling.convert.pollingcategories.PollingCategoriesConvert;
import cn.iocoder.yudao.module.polling.dal.dataobject.pollingcategories.PollingCategoriesDO;
import cn.iocoder.yudao.module.polling.dal.mysql.pollingcategories.PollingCategoriesMapper;
import cn.iocoder.yudao.module.polling.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 适用类别 Service 实现类
 *
 * @author 左达成
 */
@Service
@Validated
public class PollingCategoriesServiceImpl implements PollingCategoriesService {

    @Resource
    private PollingCategoriesMapper pollingCategoriesMapper;

    @Override
    public Integer createPollingCategories(PollingCategoriesCreateReqVO createReqVO) {
        // 插入
        PollingCategoriesDO pollingCategories = PollingCategoriesConvert.INSTANCE.convert(createReqVO);
        pollingCategoriesMapper.insert(pollingCategories);
        // 返回
        return pollingCategories.getId();
    }

    @Override
    public void updatePollingCategories(PollingCategoriesUpdateReqVO updateReqVO) {
        // 校验存在
        this.validatePollingCategoriesExists(updateReqVO.getId());
        // 更新
        PollingCategoriesDO updateObj = PollingCategoriesConvert.INSTANCE.convert(updateReqVO);
        pollingCategoriesMapper.updateById(updateObj);
    }

    @Override
    public void deletePollingCategories(Integer id) {
        // 校验存在
        this.validatePollingCategoriesExists(id);
        // 删除
        pollingCategoriesMapper.deleteById(id);
    }

    private void validatePollingCategoriesExists(Integer id) {
        if (pollingCategoriesMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.STANDARD_NOT_EXISTS);
        }
    }

    @Override
    public PollingCategoriesDO getPollingCategories(Integer id) {
        return pollingCategoriesMapper.selectById(id);
    }

    @Override
    public List<PollingCategoriesDO> getPollingCategoriesList(Collection<Integer> ids) {
        return pollingCategoriesMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PollingCategoriesDO> getPollingCategoriesPage(PollingCategoriesPageReqVO pageReqVO) {
        return pollingCategoriesMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PollingCategoriesDO> getPollingCategoriesList(PollingCategoriesExportReqVO exportReqVO) {
        return pollingCategoriesMapper.selectList(exportReqVO);
    }

    @Override
    public List<EquVo> selectAllEmp(int typeId) {
        return pollingCategoriesMapper.selectAllEmp(typeId);
    }

    @Override
    public List<EquVo> selectAllEmpType() {
        return pollingCategoriesMapper.selectAllEmpType();
    }

    @Override
    public List<EquVo> selectUserByAll(String emqName) {
        return pollingCategoriesMapper.selectUserByAll(emqName);
    }

    @Override
    public List<EquInVo> selectEquByBzId(EquInVo equInVo) {
        return pollingCategoriesMapper.selectEquByBzId(equInVo);
    }

}
